package com.cooper.core;

import java.io.IOException;

import javax.servlet.GenericServlet;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * 
* @Description: 
* @version: v1.0.0
* @author: tianlg
* @date: Mar 4, 2015 9:11:35 PM
*
* Modification History:
* Date         Author          Version            Description
*---------------------------------------------------------*
* Mar 4, 2015      tianlg          v1.0.0
 */
public class DelegatingServletProxy extends GenericServlet {
	
	private static final long serialVersionUID = -190849869268379462L;
	
	private String targetBean;
	private Servlet proxy;
	
	@Override
	public void service(ServletRequest arg0, ServletResponse arg1)
			throws ServletException, IOException {
		proxy.service(arg0, arg1);
	}

	@Override
	public void init() throws ServletException {
		this.targetBean = getServletName();
		getServletBean();
		proxy.init(getServletConfig());
	}
	
	@Override
	public void destroy() {
		proxy.destroy();
	}
	
	private void getServletBean() {
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
		this.proxy = (Servlet)wac.getBean(targetBean);
	}
}
